<template>
  <div class="colle">
    <van-nav-bar
      title="我的收藏"
      left-text="返回"
      left-arrow
      @click-left="back"
    />
    <van-row class="bg">
      <GoodsList
        v-for="item in colletion"
        @remove="remove"
        @intoDetail="intoDetail"
        :key="item.pid"
        :isColle="isColle"
        :item="item"
      ></GoodsList>
    </van-row>
  </div>
</template>

<script>
import GoodsList from "../components/GoodsList.vue";
import "../assets/less/colle.less";
export default {
  name: "Colle",
  data() {
    return {
      colletion: [],
      isColle: false,
    };
  },
  components: {
    GoodsList,
  },
  methods: {
    back() {
      this.$router.go(-1);
    },
    intoDetail(pid) {
      return this.$router.push({
        name: "Details",
        params: { pid },
      });
    },
    findColle() {
      let tokenString = this.$cookies.get("tokenString");
      if (!tokenString) {
        return this.$router.push({ name: "Login" });
      }
      this.$toast.loading({
        message: "加载中",
        forbidClick: true,
        duration: 0,
      });

      this.$axios({
        method: "GET",
        url: "/findAllLike",
        params: {
          appkey: this.appkey,
          tokenString,
        },
      })
        .then((res) => {
          // console.log('res findAllLike ==> ', res);
          this.$toast.clear();

          if (res.data.code === 2000) {
            this.isColle = true;
            this.colletion = res.data.result;
          } else {
            this.$toast({
              message: res.data.msg,
              forbidClick: true,
              duration: 1000,
            });
          }
        })
        .catch((err) => {
          this.$toast.clear();
          console.log("err ==> ", err);
        });
    },
    remove(pid) {
      let tokenString = this.$cookies.get("tokenString");
      if (!tokenString) {
        return this.$router.push({ name: "Login" });
      }
      this.$toast.loading({
        message: "加载中...",
        forbidClick: true,
        duration: 0,
      });
      this.$axios({
        method: "POST",
        url: "/notlike",
        data: {
          appkey: this.appkey,
          pid,
          tokenString,
        },
      })
        .then((res) => {
          // console.log("res noCollect ==> ", res);
          this.$toast.clear();

          if (res.data.code == 700) {
            this.$toast({
              message: res.data.msg,
              forbidClick: true,
              duration: 1000,
            });
            return this.$router.push({ name: "Login" });
          }

          if (res.data.code === 900) {
            if (res.data.result === 1) {
              this.like = false;
            }
            this.findColle();
          } else {
            this.$toast({
              message: res.data.msg,
              forbidClick: true,
              duration: 1000,
            });
          }
        })
        .catch((err) => {
          this.$toast.clear();
          console.log("err ==> ", err);
        });
    },
  },
  created() {
    this.findColle();
  },
};
</script>

<style lang="less" scoped>
</style>